import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_reader/dao/user_callBack_data_manager.dart';
import 'package:flutter_reader/pages/read_book/book_introduction.dart';
import 'package:flutter_reader/tools/rich_alert.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:overlay_support/overlay_support.dart';

class FeedbackPage extends StatefulWidget {
  FeedbackPage({Key key}) : super(key: key);

  @override
  _FeedbackPageState createState() {
    return _FeedbackPageState();
  }
}

class _FeedbackPageState extends State<FeedbackPage> {

  final _textController = TextEditingController();
  @override
  void initState() {
    super.initState();
    // initPlatformState();
  }

  @override
  void dispose() {
    _textController.dispose();
    super.dispose();
  }

  ///集成极光推送
  String debugLable = 'Unknown';
  // final JPush jpush = new JPush();

  // Future<void> initPlatformState() async {
  //   String platformVersion;
  //   try {
  //     jpush.addEventHandler(
  //       onOpenNotification: (Map<String, dynamic> message) async {
  //         print('通知栏打开');
  //         print("flutter onOpenNotification: $message");
  //         print("${message["extras"]}");
  //         print("${message["extras"]["cn.jpush.android.EXTRA"]}");
  //         Map aa = json.decode(message["extras"]["cn.jpush.android.EXTRA"]);
  //         print("${aa["bookID"]}");
  //         Navigator.push(context, MaterialPageRoute(
  //             builder: (context) => BookInfoPage(bookId: aa["bookID"],channel: "1",)
  //         ));
  //       },
  //     );
  //   } on PlatformException {
  //     platformVersion = 'Failed to get platform version.';
  //   }
  // }

  postMsgl(Map msg){
    callBackDao.postSuggestion(msg).then((value){
      if(value == true){
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return RichAlertDialog( //uses the custom alert dialog
                alertTitle: richTitle("反馈成功"),
                alertSubtitle: richSubtitle("我们已收到您的问题，将及时处理"),
                alertType: RichAlertType.SUCCESS,
              );
            }
        );
        _textController.clear();
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xffe53935),
        title: Text('问题反馈'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
            height: ScreenUtil().setHeight(600),
            color: Color(0xffeeeeee),
            padding: EdgeInsets.all(10.0),
            child: new ConstrainedBox(
              constraints: BoxConstraints(
                maxHeight: ScreenUtil().setHeight(600),
              ),
              child: new Scrollbar(
                child: new SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  reverse: true,
                  child: SizedBox(
                    height: ScreenUtil().setHeight(550),
                    child: new TextField(
                      controller: _textController,
                      maxLines: 100,
                      decoration: new InputDecoration(
                        border: InputBorder.none,
                        hintText: '请在此处编写您意见',
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(60)),
            child: SizedBox(
              width: ScreenUtil().setWidth(1060),
              height: ScreenUtil().setHeight(150),
              child: RaisedButton(
                shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.all(Radius.circular(10)),
    ),
                onPressed: (){
                  ///提交意见
                  print(_textController.text);
                  if(_textController.text == ''){
                    toast('请先填写您的意见');
//                    showGeneralDialog(
//                        context: context,
//                        barrierDismissible: true,
//                        barrierLabel: MaterialLocalizations.of(context)
//                            .modalBarrierDismissLabel,
//                        barrierColor: Colors.black45,
//                        transitionDuration: const Duration(milliseconds: 200),
//                        pageBuilder: (BuildContext buildContext,
//                            Animation animation,
//                            Animation secondaryAnimation) {
//                          return Material(
//                            color: Colors.black38,
//                            child: Center(
//                              child: Container(
//                                decoration: BoxDecoration(
//                                  borderRadius: BorderRadius.all(Radius.circular(10)),
//                                  color: Colors.white,
//                                ),
//                                width: ScreenUtil().setWidth(1000),
//                                height: ScreenUtil().setHeight(900),
//                                padding: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
//                                child: Column(
//                                  children: [
//                                    _getMainItem("26867AD8CDC2703E468980839EEFFEFA", "http://book-98nice.oss-cn-hangzhou.aliyuncs.com/DSYL00/26867AD8CDC2703E468980839EEFFEFA/cover.jpg", "香辣女校生活", "已完结", "我有个龙凤胎姐姐，因为和她长得很像，在她出了车祸后，我决定男扮女装顶替姐姐上女校，延续她的大学梦！住进女生宿舍后，我每天和女同学一起上学，一起住宿，还一起……", "都市娱乐", 26462),
//                                    Container(
//                                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(60)),
//                                      width: ScreenUtil().setWidth(900),
//                                      child: Row(
//                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
//                                        children: <Widget>[
//                                          RaisedButton(
//                                            shape: new RoundedRectangleBorder(
//                                                borderRadius: new BorderRadius.circular(15.0),
//                                                side: BorderSide(width: 1,color: Colors.redAccent)
//                                            ),
//                                            onPressed: () {},
//                                            color: Colors.white,
//                                            textColor: Colors.redAccent,
//                                            child: Container(
//                                              width: ScreenUtil().setWidth(300),
//                                              child: Center(
//                                                child: Text("加入书架".toUpperCase(),
//                                                    style: TextStyle(fontSize: ScreenUtil().setSp(40))),
//                                              ),
//                                            ),
//                                          ),
//                                          RaisedButton(
//                                            shape: new RoundedRectangleBorder(
//                                                borderRadius: new BorderRadius.circular(18.0),
//                                                side: BorderSide(color: Colors.red)),
//                                            onPressed: () {
//                                              Navigator.pop(context);
//                                            },
//                                            color: Colors.red,
//                                            textColor: Colors.white,
//                                            child: Container(
//                                              width: ScreenUtil().setWidth(300),
//                                              child: Center(
//                                                child: Text("继续阅读".toUpperCase(),
//                                                    style: TextStyle(fontSize: ScreenUtil().setSp(40))),
//                                              ),
//                                            ),
//                                          ),
//                                        ],
//                                      ),
//                                    )
//                                  ],
//                                ),
//                              ),
//                            ),
//                          );
//                        });
                  }
                  else
                    {
                      postMsgl({'msg':_textController.text});
                    }
                },
                textColor: Colors.white,
                child: Text('提交',
                style: TextStyle(fontSize: ScreenUtil().setSp(50)),),
              ),
            )
          )
        ],
      ),
    );
  }

  _getMainItem(String bookId,String imageName, String title, String state, String introduce, String type, int readTimes){
    return InkWell(
      onTap: (){
        Navigator.push(context, MaterialPageRoute(
            builder: (context) => BookInfoPage(channel: "1",bookId: bookId,bookName: title,bookImage: imageName,isHorizontal: false,hasCollect: false,)
        ));
      },
      child: Container(
        height: 1.27*ScreenUtil().setWidth(280) + ScreenUtil().setHeight(80),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                boxShadow: <BoxShadow>[
                  new BoxShadow(
                    color: Colors.black26,
                    blurRadius: 5.0,
                    spreadRadius: 1.0,
                    offset: Offset(-2.0, 2.0),
                  ),
                ],
              ),
              margin: EdgeInsets.only(
                  left:ScreenUtil().setWidth(20),
                  top:ScreenUtil().setHeight(30),
                bottom: ScreenUtil().setHeight(40)
                  ),
              child: Image(
                width: ScreenUtil().setWidth(280),
                height: 1.27*ScreenUtil().setWidth(280),
                image: NetworkImage(imageName),
                fit: BoxFit.fill,
              ),
            ),
            _rightItem(title, state, introduce, type, readTimes)
          ],
        ),
      ),
    );
  }

  _rightItem(String title, String state, String introduce, String type, int readTimes){
    return Container(
      height: 1.27*ScreenUtil().setWidth(280),
      margin: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            width: ScreenUtil().setWidth(500),
            height: ScreenUtil().setHeight(100),
            child:  Text(
              title,
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(50),
                  fontWeight: FontWeight.w500
              ),),
          ),
          Container(
              width: ScreenUtil().setWidth(600),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    width:ScreenUtil().setWidth(200),
                    child: Align(
                      alignment: Alignment.topLeft,
                      child: FittedBox(
                        fit: BoxFit.contain,
                        child: Text(
                          '[${state}]:',
                          style: TextStyle(
                              color: state == '连载中' ? Colors.lightBlue : Colors.orangeAccent
                          ),
                        ),
                      ),
                    ),
                  ),
                  Container(
                      width: ScreenUtil().setWidth(400),
                      child: Text(
                        introduce,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 2,
                        style: TextStyle(
                            color: Colors.black54,
                            fontSize: ScreenUtil().setSp(40)
                        ),
                      )
                  )
                ],
              )
          ),
          _bottom(type, readTimes)
        ],
      ),
    );
  }

  _bottom(String type, int readTimes){
    return Container(
        child: Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top:ScreenUtil().setHeight(50),right: ScreenUtil().setWidth(250)),
              width: ScreenUtil().setWidth(150),
              decoration: BoxDecoration(
                  border: Border.all(
                      color: Colors.grey,
                      width: 1.0,
                      style: BorderStyle.solid
                  ),
                  borderRadius:BorderRadius.all(Radius.circular(5.0))
              ),
              child: Text(
                type,
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(30),
                    color: Colors.grey
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(0, ScreenUtil().setHeight(60), ScreenUtil().setWidth(20), 0),
              child: Image(
                width: ScreenUtil().setWidth(50),
                height: ScreenUtil().setHeight(30),
                fit: BoxFit.fill,
                image: AssetImage('images/fl_eye.png'),
              ),
            ),
            Container(
              margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(0), ScreenUtil().setHeight(60), 0, 0),
              child: Text(
                readTimes.toString(),
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(35),
                    color: Color(0xffe53935)
                ),
              ),
            )
          ],
        )
    );
  }
}

